package com.example.myapplication2;

import android.content.Intent;
import android.content.IntentFilter;
import android.view.View;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import com.example.myapplication2.Receiver.StandardReceiverA;
import com.example.myapplication2.Receiver.StandardReceiverB;
import com.example.myapplication2.Receiver.StandardReceiverC;

public class OrderBroadcastStandardActivity extends AppCompatActivity implements View.OnClickListener {

    private StandardReceiverA standardReceiverA;
    private StandardReceiverB standardReceiverB;
    private StandardReceiverC standardReceiverC;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_broadcast_standard);
        findViewById(R.id.sendOrderBroadcastStandard).setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.sendOrderBroadcastStandard:
                System.out.println("发送有序广播");
                Intent intent = new Intent("OrderBroadcastStandard");
                //发送有序广播
                sendOrderedBroadcast(intent,null);
                break;
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        //创建接收器
        standardReceiverA = new StandardReceiverA();
        standardReceiverB = new StandardReceiverB();
        standardReceiverC = new StandardReceiverC();

        //创建意图过滤器
        IntentFilter intentFilterA = new IntentFilter("OrderBroadcastStandard");
        IntentFilter intentFilterB = new IntentFilter("OrderBroadcastStandard");
        IntentFilter intentFilterC = new IntentFilter("OrderBroadcastStandard");
        //设置意图优先级别
        intentFilterA.setPriority(10);
        intentFilterB.setPriority(8);
        intentFilterC.setPriority(20);
        //注册接收者和意图过滤器
        registerReceiver(standardReceiverB, intentFilterB);
        registerReceiver(standardReceiverA, intentFilterA);
        registerReceiver(standardReceiverC, intentFilterC);
    }

    @Override
    protected void onStop() {
        super.onStop();
        unregisterReceiver(standardReceiverB);
        unregisterReceiver(standardReceiverA);
        unregisterReceiver(standardReceiverC);
    }
}